<template>
	<view class="container goodsList-container">
		<view v-if="pageKey" style="font-size: 24rpx;color: #666;padding: 20rpx;">
			搜索：'{{pageKey}}'
		</view>
		<view v-else-if="pageCoupon" style="font-size: 24rpx;color: #666;padding: 20rpx;">
			优惠券：'{{pageCouponName}}'
		</view>
		<template v-else>
			<view class="top-filter">
				<view class="select-panel" style="margin-right: 30rpx;">
					<picker class="setting-picker" mode="selector" :range="classList" range-key="name"
						@change="categoryOneChange" :value="categoryOneKey">
						<view class="rent-picker">
							<view class="picker-input selected-input" v-if="paramsData.categoryOneCode">
								{{paramsData.categoryOneName}}
							</view>
							<view class="picker-input placeholder-style" v-else>一级分类</view>
							<text class="iconfont">&#xe613;</text>
						</view>
					</picker>
				</view>
				<view class="select-panel" style="flex-grow: 1;width: 200rpx;overflow: auto;">
					<view class="child-class" :class="paramsData.categoryTwoCode===item.id&&'child-class-sel'"
						v-for="item in childClassList" @click="categoryTwoChange(item)">{{item.name}}</view>
					<!-- <picker class="setting-picker" mode="selector" :range="childClassList" range-key="name"
						@change="categoryTwoChange" :value="categoryTwoKey">
						<view class="rent-picker">
							<view class="picker-input selected-input" v-if="paramsData.categoryTwoCode">
								{{paramsData.categoryTwoName}}
							</view>
							<view class="picker-input placeholder-style" v-else>二级分类</view>
							<text class="iconfont">&#xe613;</text>
						</view>
					</picker> -->
				</view>
			</view>
			<view class="top-filter top-solt">
				<view class="select-panel">
					<picker class="setting-picker" mode="selector" :range="soltArr" range-key="name"
						@change="soltArrChange">
						<view class="rent-picker">
							<view class="picker-input selected-input">
								{{paramsData.soltArrName}}
							</view>
							<text class="iconfont">&#xe613;</text>
						</view>
					</picker>
				</view>
				<view class="select-panel">
					<picker class="setting-picker" mode="selector" :range="brandArr" range-key="name"
						@change="brandArrChange">
						<view class="rent-picker">
							<view class="picker-input selected-input" v-if="paramsData.brandArrCode">
								{{paramsData.brandArrName}}
							</view>
							<view class="picker-input placeholder-style" v-else>品牌</view>
							<text class="iconfont">&#xe613;</text>
						</view>
					</picker>
				</view>
				<view class="select-panel">
					<view class="rent-picker" @click="showOtherFilter=true">
						<view class="picker-input placeholder-style">筛选</view>
						<text class="iconfont">&#xe613;</text>
					</view>
				</view>
			</view>
		</template>


		<view class="goods-box">
			<goodsListVue :goodsList="pageList" />
			<view class="not-more" v-if="isLoading">加载中...</view>
			<view class="null-page" v-else-if="pageList.length<=0 ">
				<text class="iconfont">&#xe642;</text>
				<view class="null-page-tips">暂无数据~</view>
			</view>
			<view class="not-more" v-else-if="notMore">没有更多了...</view>
		</view>

		<view class="other-panel" :class="showOtherFilter&&'other-panel-show'">
			<view class="other-main" :class="showOtherFilter&&'other-main-show'">
				<view class="other-title">
					<text>筛选</text>
					<text class="iconfont" @click="showOtherFilter=false">&#xe62c;</text>
				</view>
				<view class="other-list">
					<view class="list-title">价格</view>
					<view class="list-content">
						<input class="list-input" v-model="otherData.minPrice" type="number"
							@confirm="otherPanelSure" />
						<text style="color: #666;margin: 0 20rpx;">--</text>
						<input class="list-input" v-model="otherData.maxPrice" type="number"
							@confirm="otherPanelSure" />
					</view>
				</view>
				<view class="other-bottom">
					<view class="yuan-btn btn-default reset-btn" @click="resetOtherData()">重置</view>
					<view class="yuan-btn btn-success sure-btn" @click="showOtherFilter=false;refreshList()">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, reactive, ref, nextTick } from 'vue';
	import goodsListVue from '@/components/goodsList.vue'
	import { onLoad, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app"
	import { hideLoading, showLoading } from '../../common/util';
	import api from '../../common/api';
	const showOtherFilter = ref(false)

	const isLoading = ref(false)
	const pageList = ref([])
	const notMore = ref(true)
	const soltArr = [
		{ name: '综合', code: '' },
		{ name: '销量由高到低', code: 'SVIO' },
		{ name: '最新上架', code: 'GTPS' },
		{ name: '价格升序', code: 'PPS' },
		{ name: '价格降序', code: 'PIO' },
	]
	const brandArr = ref([])
	const paramsData = reactive({
		categoryOneName: '',
		categoryOneCode: undefined,
		categoryTwoName: '',
		categoryTwoCode: undefined,
		soltArrName: '综合',
		soltArrCode: '',
		brandArrName: '',
		brandArrCode: undefined,
	})
	const otherData = reactive({
		minPrice: '',
		maxPrice: ''
	})
	const resetOtherData = () => {
		otherData.minPrice = ''
		otherData.maxPrice = ''
	}
	const categoryOneKey = ref(0)
	const categoryTwoKey = ref(0)
	const categoryOneChange = (e : any) => {
		const find = classList.value[e.detail.value]
		const data = JSON.parse(JSON.stringify({ ...find }))
		const dataChildren = data.children
		if (find) {
			paramsData.categoryOneCode = data.id
			paramsData.categoryOneName = data.name

			childClassList.value = []
			const child = data.children[0]
			categoryTwoKey.value = 0
			paramsData.categoryTwoCode = child.id
			paramsData.categoryTwoName = child.name
			nextTick(() => {
				childClassList.value = dataChildren
			})
		}
		refreshList()

	}
	const categoryTwoChange = (item) => {
		paramsData.categoryTwoCode = item.id
		paramsData.categoryTwoName = item.name
		paramsData.brandArrCode = undefined
		paramsData.brandArrName = ''
		findAllBrand()
		refreshList()
	}
	const soltArrChange = (e : any) => {
		const find = soltArr[e.detail.value]
		if (find) {
			paramsData.soltArrCode = find.code
			paramsData.soltArrName = find.name
		}
		refreshList()

	}
	const brandArrChange = (e : any) => {
		const find = brandArr.value[e.detail.value]
		if (find.code === '-1') {
			paramsData.brandArrCode = undefined
			paramsData.brandArrName = ''
		} else {
			paramsData.brandArrCode = find.code
			paramsData.brandArrName = find.name
		}

		refreshList()
	}
	const otherPanelSure = () => {

	}



	const pageKey = ref('')
	const pageCoupon = ref('')
	const pageCouponName = ref('')
	onLoad((options) => {
		pageKey.value = options.keyword;
		pageCoupon.value = options.couponId;
		pageCouponName.value = options.name;

		paramsData.categoryOneCode = options.level1
		paramsData.categoryTwoCode = options.level2
	})
	onMounted(async () => {
		if (pageKey.value || pageCoupon.value) {
			getPageList()
		} else {
			await getClassList()
			getPageList()
			findAllBrand()
		}

	})
	const pageSize = 10
	let pageNum = 1
	const getPageList = async () => {
		showLoading()
		isLoading.value = true
		try {
			let res = null
			let data = null
			if (pageKey.value) {
				//如果是搜索有关键字
				res = await api.findGoodsInfoByName({
					pageNum, pageSize, goodsName: pageKey.value, auditStatus: 1, status: 1
				})
				data = res.data && res.data.rows ? res.data : { rows: [] }
			} else if (pageCoupon.value) {
				//如果是按优惠券查询
				res = await api.findUsableGoodsByCouponId({
					pageNum,
					pageSize,
					couponId: pageCoupon.value, auditStatus: 1, status: 1
				})
				data = res.data && res.data.rows ? res.data : { rows: [] }
			} else if (paramsData.categoryTwoCode) {
				//如果是按分类查询
				res = await api.findGoodsInfoByClassify({
					pageNum,
					pageSize,
					brand: paramsData.brandArrName,
					synthesizeCondition: paramsData.soltArrCode,
					lowestPrice: Number(otherData.minPrice) * 100 || '',
					highestPrice: Number(otherData.maxPrice) * 100 || '',
					twoClassifyId: paramsData.categoryTwoCode, auditStatus: 1, status: 1
				})
				data = res.data && res.data.rows ? res.data : { rows: [] }
			}

			if (data.rows.length < pageSize) notMore.value = true
			pageList.value = pageList.value.concat(data.rows)
		} finally {
			isLoading.value = false
			hideLoading()
		}
	}
	const loadMore = () => {
		if (isLoading.value) return
		if (notMore.value) return
		pageNum = pageNum + 1
		getPageList()
	}
	const refreshList = () => {
		notMore.value = false
		pageNum = 1
		pageList.value = []
		getPageList()
	}
	onReachBottom(() => {
		loadMore()
	})
	onPullDownRefresh(() => {
		refreshList()
		uni.stopPullDownRefresh()
	})

	const classList = ref([])
	const childClassList = ref([])
	const getClassList = async () => {
		showLoading()
		try {
			const res = await api.findGoodsClassify({})
			const rows = res.data?.rows || [];
			const data = {};
			rows.forEach((item) => {
				if (item.level === "一级") {
					if (data[item.id]) {
						data[item.id].name = item.name;
					} else {
						data[item.id] = {
							id: item.id + '',
							name: item.name,
							children: [],
						};
					}
				} else {
					if (data[item.parentId]) {
						data[item.parentId].children.push({ id: item.id + '', name: item.name, picture: item.picture });
					} else {
						data[item.parentId] = {
							id: item.parentId + '',
							name: "",
							children: [{ id: item.id + '', name: item.name, picture: item.picture }],
						};
					}
				}
			});
			const dataArr : any[] = [];
			for (let i in data) {
				dataArr.push(data[i]);
			}
			classList.value = dataArr


			if (paramsData.categoryOneCode) {
				const find = classList.value.find(item => {
					return item.id === paramsData.categoryOneCode
				})
				if (find) {
					paramsData.categoryOneName = find.name
					childClassList.value = find.children
				}
			} else {
				childClassList.value = dataArr[0].children
			}
			if (paramsData.categoryTwoCode) {
				const find = childClassList.value.find(item => {
					return item.id === paramsData.categoryTwoCode
				})
				if (find) {
					paramsData.categoryTwoName = find.name
				}
			} else {
				const find = childClassList.value[0]
				if (find) {
					paramsData.categoryTwoCode = find.id
					paramsData.categoryTwoName = find.name
				}
			}


		} finally {
			hideLoading()
		}
	}

	const findAllBrand = async () => {
		const res = await api.findAllBrand({
			categoryId:paramsData.categoryTwoCode
		})
		brandArr.value = res.data.map((item, idx) => {
			return { name: item, code: idx + 1 }
		})
		brandArr.value.unshift({ name: '全部', code: '-1' })
	}
</script>

<style lang="scss">
	.goodsList-container {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.top-filter {
			display: flex;
			align-items: center;
			background-color: #fff;
			padding: 20rpx 40rpx;
		}

		.goods-box {
			flex-grow: 1;
			overflow: auto;
			padding: 0 20rpx;
			padding-bottom: 120rpx;
			margin-top: 20rpx;
		}

		.top-solt {
			justify-content: space-between;
		}

		.select-panel {
			display: flex;
			align-items: center;
			flex-shrink: 0;
		}

		.child-class {
			font-size: 26rpx;
			margin-right: 20rpx;
			flex-shrink: 0;
		}

		.child-class-sel {
			color: #FF315A
		}

		.rent-picker {
			display: flex;
			align-items: center;

			.iconfont {
				font-size: 24rpx;
				margin-left: 10rpx;
			}
		}

		.picker-input {
			font-size: 30rpx;
		}

		.selected-input {
			color: #333;
		}

		.top-solt {
			border-top: solid 1px #eee;
		}




		.other-panel {
			position: fixed;
			background-color: rgba(0, 0, 0, 0.3);
			width: 100%;
			height: 100%;
			left: 0;
			top: 0;
			transform: translateY(100%);
		}

		.other-panel-show {
			transform: translateY(0);
		}

		.other-main {
			background-color: #fff;
			border-radius: 15rpx 15rpx 0 0;
			position: absolute;
			left: 0;
			bottom: 0;
			width: 100%;
			padding: 0 20rpx 50rpx 20rpx;
			transform: translateY(100%);
			transition: all 0.3s;
		}

		.other-main-show {
			transform: translateY(0);
		}

		.other-title {
			color: #333;
			font-size: 32rpx;
			text-align: center;
			position: relative;
			line-height: 80rpx;
			font-weight: bold;

			.iconfont {
				position: absolute;
				right: 0;
				top: 0;
				font-size: 32rpx;
				padding: 5rpx;
				font-weight: lighter;
			}
		}

		.other-list {
			.list-content {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
			}

			.list-input {
				border: solid 1px #eee;
				border-radius: 10rpx;
				padding: 10rpx 15rpx;
				width: 200rpx;
				font-size: 24rpx;
			}
		}

		.list-title {
			font-size: 28rpx;
			color: #333;
			font-weight: bold;
		}

		.other-bottom {
			display: flex;
			align-items: center;
			margin-top: 100rpx;
			justify-content: center;

			.reset-btn {
				width: 180rpx;
				height: 70rpx;
				margin-right: 50rpx;
				border-radius: 70rpx;
			}

			.sure-btn {
				width: 200rpx;
				height: 70rpx;
				border-radius: 70rpx;
			}
		}
	}
</style>